package com.wwlwxg.spring_security.servlet;

import com.wwlwxg.spring_security.servlet.MyFilter;
import com.wwlwxg.spring_security.servlet.MyListener;
import com.wwlwxg.spring_security.servlet.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * 有这个类就不需要在 servlet 上加 注解
 */
@Configuration
public class MyRegisterConfig {

  @Bean
  public ServletRegistrationBean myServlet() {
    MyServlet servlet = new MyServlet();

    return new ServletRegistrationBean(servlet, "/my", "/my02");
  }

  @Bean
  public FilterRegistrationBean myFilter() {
    MyFilter myFilter = new MyFilter();

//    return new FilterRegistrationBean(myFilter, myServlet());   // 过滤 ServletRegistrationBean 的 urlMapping

    FilterRegistrationBean bean = new FilterRegistrationBean(myFilter);
    bean.setUrlPatterns(Arrays.asList("/my", "/css/*"));

    return bean;
  }

  @Bean
  public ServletListenerRegistrationBean myListener() {
    MyListener myListener = new MyListener();

    return new ServletListenerRegistrationBean(myListener);
  }
}
